<?php
function getUt($str){
	//pegando url do canal
	$i = strpos($str, "href");
	$f = strpos($str, "\" class");
	$username = substr($str,($i+12),($f-12)-$i);
	$i = strpos($str, "title")+7;
	$f = strpos($str, "\" dat");
	$titulo = substr($str,$i,$f-$i);
	return array("username"=> $username, "titulo"=> $titulo);
	
}

function getMini1(){
	global $res;
	global $ohtml;

	for ($i=0; $i < count($res); $i++) {
		$i1 = strpos($ohtml, "src=\"https://yt3.gg");
		$ohtml = substr($ohtml, $i1);
		$i1 = 0;
		$i2 = strpos($ohtml, ".jpg\"")+5;
		$res[$i]["mini"] = substr($ohtml, $i1+5, $i2-6);
		$ohtml = substr($ohtml, $i2+30);
	}
}


function search(){
global $res;
global $html;
global $ohtml;
	
$a = strpos($html, "<a dir=\"ltr");
$i = 0;
$c = 0;
while ($a!='' and $c<10) {
	$html = substr($html, $a);
	$res[$i] = getUt(substr($html, 1));
	$html = substr($html, 150);
	$a = strpos($html, "<a dir=\"ltr");
	$i = $i+1;
	$c = $c+1;
}
getMini1();
$st = "";
for ($i=0; $i < count($res); $i++) { 
	$tit = "<h2>".$res[$i]["titulo"]."</h2>";
	$mi = "<img src=\"".$res[$i]["mini"]."\" class=\"img-thumbnail\" style=\"float:left;margin:4px 4px;\">";
	$us = "<h3 style=\"display: none;\">".$res[$i]["username"]."</h3>";
	$st = $st."<div class=\"col-md-6\">".$mi."<div class=\"col-sm-7\">".$tit.$us."<button type=\"button\" class=\"btn-add active btn btn-lg btn-success\" style=\"padding-left: 30px;padding-right: 30px;\">+Adicionar</button></div></div>";
}
	
echo "<br>".$st;
}





//MAIN
$c = $_POST['canal'];
$res = array();
$html = "";
$ohtml = "";
if($c==""){
	echo "<h1 style=\"color:red;\">Erro - Preencha o campo acima</h1>";
}else{
	$html = file_get_contents("https://www.youtube.com/channels?q=".$c."");
	$ohtml = $html;
	search();
}

?>